package  
{
	import org.flixel.FlxPoint;
	import org.flixel.FlxSprite;
	import flash.display.*;

	/**
	 * ...
	 * @author Shrubbles
	 */
	public class Spherobot extends FlxSprite
	{
		[Embed(source = "../assets/spherobot.PNG")]  static public var sphero_img:Class;
		public var resultForce:FlxPoint;
		public var externalForces:FlxPoint;
		public var gravity:Number;
		public var gravityForce:Number;
		
		public function Spherobot(x:Number = 0, y:Number = 0, width:uint = 10, height:uint = 10, simpleGraphic:Class = null, color:uint=0xffffffff)
		{
			super(x, y, sphero_img);
			//this.makeGraphic(width, height, color);
			setGravity(); // 200 (Default)
			
			resultForce = new FlxPoint();
			externalForces = new FlxPoint();
			
			setDrag(0,1);
			setMaxVelocity(80, 200);
			setGravity(10);
			mass = 1;
		}
		
		public function setGravity(g:Number = 200)
		{
			this.gravity = g;
		}
		
		public function setMaxVelocity(x:Number = 0, y:Number = 0)
		{
			this.maxVelocity.x = x;
			this.maxVelocity.y = y;
		}
		
		
		
		public function setDrag(x:Number = 0, y:Number = 0)
		{
			this.drag.x = x;
			this.drag.y = y;
		}
		
		
		public function addExternalForcesToResultant()
		{
			resultForce.x += externalForces.x;
			resultForce.y += externalForces.y;
		}
		
		public function addGravityForceToResultant()
		{
			// adicionando o peso
			resultForce.y = (mass * gravity);
		}
		
		public function calculateAcceleration():void
		{
			this.acceleration.x += this.resultForce.x / mass;
			this.acceleration.y += this.resultForce.y / mass;
			
		}
		
		// Adiciona forcas externas
		public function addExternalForce(force:FlxPoint):void
		{
			externalForces.x += force.x;
			externalForces.y += force.y; 
									

		}
	
	/*	override public function preUpdate():void
		{
			// preUpdate
			
		}
		
		override public function postUpdate():void
		{
			// posUpdate
			
			
		}*/
		
		override public function update():void
		{
			addGravityForceToResultant();
			addExternalForcesToResultant();
			calculateAcceleration();
		}
		
		public function zeroForces()
		{
			externalForces.x = 0;
			externalForces.y = 0;
			resultForce.x = 0;
			resultForce.y = 0;
		}
		
		// tocou o chão = parar de somar gravidade
		public function hasHitFloor()
		{
			acceleration.y = 0;
		}
		
		// tocou a parede = parar de somar acc horizontais
		public function hasHitWall()
		{
			acceleration.x = 0;
		}
	}

}
